package com.jd.header;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.jd.header.databinding.ActivityMainBinding;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mainBinding;
    private int maxTop = 0;
    private int top30 = 0;
    private int top60 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mainBinding.getRoot());
        initView();
        initData();
        initLister();
    }

    private void initView() {
        mainBinding.refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        mainBinding.refreshLayout.setEnableLoadMore(false);
    }

    private void initData() {
        maxTop = DensityUtil.dp2px(MainActivity.this, 72);
        top30 = DensityUtil.dp2px(MainActivity.this, 30);
        top60 = DensityUtil.dp2px(MainActivity.this, 60);
    }

    private void initLister() {
        mainBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mainBinding.refreshLayout.finishRefresh();
                    }
                }, 1000);
            }
        });
        mainBinding.appbar.addOnOffsetChangedListener(mAppBarStateChangeListener);
        mainBinding.imgUserHeadTop.setVisibility(View.INVISIBLE);
        mainBinding.imgSlide.post(new Runnable() {
            @Override
            public void run() {

                /*
                  Y坐标
                  maxTop 72
                  minTop 42
                 */
                mainBinding.imgSlide.setX(DensityUtil.dp2px(MainActivity.this, 16));
                mainBinding.imgSlide.setY(DensityUtil.dp2px(MainActivity.this, 72));

            }
        });
    }

    private final HomeAppBarStateChangeListener mAppBarStateChangeListener = new HomeAppBarStateChangeListener() {

        @Override
        public void onStateChanged(AppBarLayout appBarLayout, State state, int slidingDistance) {
            switch (state) {
                case EXPANDED:      //展开
                    setTitleBackground(1);
                    mainBinding.imgUserHeadTop.setVisibility(View.INVISIBLE);
                    mainBinding.imgSlide.setVisibility(View.INVISIBLE);
                    mainBinding.imgUserHead.setVisibility(View.VISIBLE);
                    mainBinding.layoutUserInfoTop.setAlpha(0f);
                    break;
                case IDLE:          //滚动
                    mainBinding.imgUserHead.setVisibility(View.INVISIBLE);
                    break;
                case COLLAPSED:     //置顶
                    setTitleBackground(0);
                    mainBinding.imgUserHeadTop.setVisibility(View.VISIBLE);
                    mainBinding.imgUserHead.setVisibility(View.INVISIBLE);
                    mainBinding.imgSlide.setVisibility(View.INVISIBLE);
                    mainBinding.layoutUserInfoTop.setAlpha(1f);
                    break;
            }
            mainBinding.refreshLayout.requestLayout();
        }

        @Override
        public void onStateSliding(AppBarLayout appBarLayout, State state, int slideHeight) {
            int slidingHeight = Math.abs(slideHeight);
            if (slidingHeight <= top30 && slidingHeight >= 0) {

                if (mainBinding.imgSlide.getVisibility() != View.VISIBLE) {
                    mainBinding.imgSlide.setVisibility(View.VISIBLE);
                }
                if (mainBinding.imgUserHeadTop.getVisibility() != View.INVISIBLE) {
                    mainBinding.imgUserHeadTop.setVisibility(View.INVISIBLE);
                }

                mainBinding.imgSlide.setY(maxTop - slidingHeight);

                if (slidingHeight <= top30) {
                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mainBinding.imgSlide.getLayoutParams();
                    layoutParams.width = top60 - slidingHeight;
                    layoutParams.height = top60 - slidingHeight;
                    mainBinding.imgSlide.requestLayout();
                }

                Glide.with(MainActivity.this)
                        .load(R.mipmap.icon_login_default_header)
                        .into(mainBinding.imgSlide);

            } else {
                if (mainBinding.imgUserHeadTop.getVisibility() != View.VISIBLE) {
                    mainBinding.imgUserHeadTop.setVisibility(View.VISIBLE);
                }
                if (mainBinding.imgSlide.getVisibility() != View.INVISIBLE) {
                    mainBinding.imgSlide.setVisibility(View.INVISIBLE);
                }
            }

            if (slidingHeight < 100) {
                float alpha = slidingHeight * 1.0f / 100f;

                if (alpha < 0.3) {
                    setTitleBackground(1);
                } else {
                    setTitleBackground(0);
                }

                mainBinding.layoutUserInfoTop.setAlpha(alpha);
            } else {
                setTitleBackground(0);
                mainBinding.layoutUserInfoTop.setAlpha(1f);
            }
        }
    };

    /**
     * 设置状态栏字体颜色
     *
     * @param titleBackground 1黑色  0白色
     */
    public void setTitleBackground(int titleBackground) {
        if (titleBackground == 1) {//深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else if (titleBackground == 0) {//浅色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }
}